<template>
   <el-container class="index">
     <!-- 头部 -->
    <el-header>
      <div class="logo">
        <img src="../assets/logo.png" alt="">
      </div>
      <div class="title">
        <h1>电商后台管理系统</h1>
      </div>
      <div class="logout">
        欢迎光临~
        <a href="javascript:;" @click ='logout'>退出</a>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <el-menu
        router
    :default-active='defaultActive'
    unique-opened
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b">

    <el-submenu :index="menu.path" v-for='menu in menuList' :key='menu.id'>
        <!-- 标题 -->
        <template v-slot:title>
            <i class="el-icon-location"></i>
            <span> {{ menu.authName }} </span>
        </template>

        <el-menu-item :index="item.path" v-for='item in menu.children' :key='item.id'>
            <i class="el-icon-menu"></i>
            <span slot="title">{{ item.authName }} </span>
        </el-menu-item>
    </el-submenu>

</el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>

import axios from 'axios'

export default {
  computed: {
    defaultActive () {
      return this.$route.path.slice(1)
    }
  },
  // config 是一个配置对象, 里面可以配置请求头, params地址栏的参数
  // get/delete, 请求的参数, 在地址栏传递的, 需要放在params中
  // axios.get(url, config)
  // axios.delete(url, config)

  // // post/put/patch 请求的参数, 在请求体中传递的, 放在data中
  // axios.post(url, data, config)
  // axios.put(url, data, config)
  // axios.patch(url, data, config)
  data () {
    return {
      menuList: []
    }
  },
  created () {
    // 发送ajax获取menuList进行渲染
    axios.get('http://localhost:8888/api/private/v1/menus', {
      headers: {
        Authorization: localStorage.getItem('token')
      }
    }).then(res => {
      const { data, meta } = res.data
      if (meta.status === 200) {
        this.menuList = data
        console.log(this.menuList)
      } else {
        this.$message.error(meta.msg)
      }
    })
  },

  methods: {
    logout () {
      // this.$confirm
      // 参数1: 提示消息
      // 参数2: 标题
      // 参数3: 配置对象
      this.$confirm('亲, 你确认要退出系统吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        // 确认提示
        this.$message.success('恭喜, 退出成功')
        // 从本地清除 token
        localStorage.removeItem('token')
        // 跳转登录路由
        this.$router.push('/login')
      }).catch(() => {
        // 取消
        this.$message('取消退出')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.index {
  height: 100%;
   .el-header {
    background-color: #d8d8d8;
    display:flex;
    .logo{
      width: 180px;
      img{
        height: 40px;
        margin: 10px;
      }
    }
    .title{
      flex:1;
      h1{
        text-align: center;
        height: 60px;
        line-height: 60px;
        color:#545c64;
      }
    }
    .logout{
      height: 60px;
      line-height: 60px;
      text-align: right;
      font-weight: 700;
      width: 180px;
      a{
         color:orange
      }
    }
  }
  .el-aside {
    background-color: #545c64;
  }
  .el-main {
    background-color: #ecf0f1;
  }
}
</style>
